const router = require('koa-router')()
const path=require('path')
const fs=require('fs')

//查询全部
router.get('/gettravel', async (ctx, next) => {
    let sql = "select * from travel order by id DESC";
    let result = await ctx.db.EXCUTE(sql);
    for (let i in result) {
        //发布时间格式化
        let timeObj = new Date(result[i].time);
        result[i].time = timeObj.getFullYear() + '-' + (timeObj.getMonth() + 1) + '-' + timeObj.getDate() + ' '
            + timeObj.getHours() + ':' + timeObj.getMinutes() + ':' + timeObj.getSeconds();
    }
    for (let i in result) {
        let img = result[i].image;
        let header = img.split('/')[0];
        if (header == '..') {
            let arr1 = img.split('/');
            let arr2 = arr1.splice(2, 4)
            arr2 = arr2.join('/')
            //绝对地址
            let filePath = path.join(__dirname, '../../../小程序/', arr2)
            console.log(filePath)
            let fileObj = fs.readFileSync(filePath);
            let file = fileObj.toString('base64');
            let imgUrl = `data:image/png;base64,${file}`
            result[i].image = imgUrl;
        }
    }
    ctx.body = { data: result, status: "获取信息成功" }
})

//搜索
router.post('/seltravel', async (ctx, next) => {
    let userid = ctx.request.body.userid;
    let sql = `select * from travel where userid like'%${userid}%' order by id DESC`;
    let result = await ctx.db.EXCUTE(sql, [userid]);
    if (result.length != 0) {
        for (let i in result) {
            //发布时间格式化
            let timeObj = new Date(result[i].time);
            result[i].time = timeObj.getFullYear() + '-' + (timeObj.getMonth() + 1) + '-' + timeObj.getDate() + ' '
                + timeObj.getHours() + ':' + timeObj.getMinutes() + ':' + timeObj.getSeconds();
        }
        for (let i in result) {
            let img = result[i].image;
            let header = img.split('/')[0];
            if (header == '..') {
                let arr1 = img.split('/');
                let arr2 = arr1.splice(2, 4)
                arr2 = arr2.join('/')
                //绝对地址
                let filePath = path.join(__dirname, '../../../小程序/', arr2)
                console.log(filePath)
                let fileObj = fs.readFileSync(filePath);
                let file = fileObj.toString('base64');
                let imgUrl = `data:image/png;base64,${file}`
                result[i].image = imgUrl;
            }
        }
        ctx.body = { code: 200, status: "搜索信息成功", data: result }
    } else {
        ctx.body = { code: 400, status: "输入信息错误,无数据", data: [] }
    }

})

//删除
router.post('/deltravel', async (ctx, next) => {
    let id = ctx.request.body.id;
    let delsql = 'delete from travel where id=?';
    let result = await ctx.db.EXCUTE(delsql, [id]);
    ctx.body = { "code": 200, "msg": "ok", status: "删除成功" };
})

//审核通过
router.post('/suctravel', async (ctx, next) => {
    let id = ctx.request.body.id;
    let state = ctx.request.body.state;
    let sql = 'update travel set state=? where id=?';
    let result = await ctx.db.EXCUTE(sql, [state, id]);
    ctx.body = { "code": 200, "msg": "ok", status: "审核通过" };
})

//审核失败
router.post('/passtravel', async (ctx, next) => {
    let id = ctx.request.body.id;
    let state = ctx.request.body.state;
    let sql = 'update travel set state=? where id=?';
    let result = await ctx.db.EXCUTE(sql, [state, id]);
    ctx.body = { "code": 200, "msg": "ok", status: "审核不通过" };
})

module.exports = router